﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BenevolentSun
{

    class CreditsScreen : Screen
    {

        TextField title, spencer, spencerTitle, aj, ajTitle;

        List<string> jobTitlesAj, jobTitlesSpencer;

        Tween fader;

        List<TextField> fields = new List<TextField>();

        Sprite background;

        Sprite logo;

        InstructionsControl instructionsControl;

        DimmerControl dimmerControl;


        public CreditsScreen()
        {
            this.TransitionOffTime = new TimeSpan(0, 0, 0, 0, 100);

            jobTitlesAj = new List<string>();
            jobTitlesAj.Add("Synergy Consultant");
            jobTitlesAj.Add("Cat Owner");
            jobTitlesAj.Add("Lol Coordinator");
            //jobTitlesAj.Add("VP of Strategic Internal Development and Risk Management"); // too long
            jobTitlesAj.Add("Mascot");
            jobTitlesAj.Add("Chair of Fun Foresight Committee");
            jobTitlesAj.Add("Holiday Party Coordinator");
            jobTitlesAj.Add("Codenaut");
            jobTitlesAj.Add("Host Programmer");

            jobTitlesSpencer = new List<string>();
            jobTitlesSpencer.Add("Codenaut");
            jobTitlesSpencer.Add("Host Programmer");
            jobTitlesSpencer.Add("Chief Coffee Officer");
            jobTitlesSpencer.Add("Snuggie Enthusiast");
            jobTitlesSpencer.Add("Director of Rofl");
            jobTitlesSpencer.Add("Bit Relations Manager");
            jobTitlesSpencer.Add("Assistant to the Regional Manager");      // About max length
            jobTitlesSpencer.Add("Paint by Numbers Expert");
            jobTitlesSpencer.Add("Bakesale Manager");
            jobTitlesSpencer.Add("Blood Elf Rogue");

            //Senior something?
            //Concerned Citizen?
        }

        public override void LoadContent()
        {
            SpriteFont titleFont = Content.Load<SpriteFont>("Fonts/Screen Title");
            title = new TextField("Credits", titleFont, new Color(0, 255, 255));
            title.AlignH = AlignH.Center;
            title.Position = new Vector2(1280f / 2f, 10f);
            title.Color.A = 0;

            SpriteFont namesFont = Content.Load<SpriteFont>("Fonts/Credits Names");
            SpriteFont titlesFont = Content.Load<SpriteFont>("Fonts/Credits Titles");

            spencer = new TextField("Spencer Evans", namesFont);
            spencer.AlignH = AlignH.Left;
            spencer.Position = new Vector2(45, 385);
            spencer.Color.A = 0;
            aj = new TextField("Arthur Low", namesFont);
            aj.AlignH = AlignH.Right;
            aj.Position = new Vector2(1280 - 45, 385);
            aj.Color.A = 0;

            spencerTitle = new TextField(JobTitleTextSE, titlesFont);
            spencerTitle.AlignH = AlignH.Left;
            //spencerTitle.AlignV = AlignV.Top;
            spencerTitle.Position = new Vector2(45, 350);
            spencerTitle.Color.A = 0;
            ajTitle = new TextField(JobTitleTextAJ, titlesFont);
            ajTitle.AlignH = AlignH.Right;
            //ajTitle.AlignV = AlignV.Top;
            ajTitle.Position = new Vector2(1280 - 45, 350);
            ajTitle.Color.A = 0;

            fields.Add(title);
            fields.Add(spencer);
            fields.Add(aj);
            fields.Add(spencerTitle);
            fields.Add(ajTitle);

            logo = new Sprite(Content.Load<Texture2D>("Textures/Gui/Logo"));

            background = new Sprite(Content.Load<Texture2D>("Textures/Gui/Background"));

            instructionsControl = new InstructionsControl(Content);

            dimmerControl = new DimmerControl(Content);
            dimmerControl.FadeIn();
            dimmerControl.AdvanceToEnd();

            refreshMenuInstructions();

            FadeIn();

            base.LoadContent();
        }

        public string JobTitleTextAJ
        {
            get
            {
                return jobTitlesAj[new Random().Next(jobTitlesAj.Count)];
            }
        }

        public string JobTitleTextSE
        {
            get
            {
                return jobTitlesSpencer[new Random().Next(jobTitlesSpencer.Count)];
            }
        }

        private void refreshMenuInstructions()
        {
            instructionsControl.HideInstructions();
            instructionsControl.SetInstructions("Press any button to exit.");
            instructionsControl.FadeInInstructions();
        }


        public void FadeIn()
        {
            fader = new Tween(spencer.Color.A, (byte)(255 * 1f), 0.25, Tween.LoopType.None);
        }

        public void FadeOut()
        {
            fader = new Tween(spencer.Color.A, 0, 0.25, Tween.LoopType.None);
        }

        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            if (fader != null)
            {
                fader.Update(gameTime);
                foreach (TextField tf in fields) 
                    tf.Color.A = (byte)fader.Value;

                if (fader.IsFinished)
                    fader = null;
            }

            instructionsControl.Update(gameTime);
            dimmerControl.Update(gameTime);

            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void HandleInput()
        {
            if (ActionMap.AnyButton)
            {
                ExitScreen();
            }

            base.HandleInput();
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin();

            background.Draw(SpriteBatch);

            logo.Draw(SpriteBatch);

            dimmerControl.Draw(SpriteBatch);

            instructionsControl.Draw(SpriteBatch);

            foreach (TextField tf in fields)
                tf.Draw(SpriteBatch);

            SpriteBatch.End();
            base.Draw(gameTime);
        }

        public override void ExitScreen()
        {
            FadeOut();
            instructionsControl.FadeOutInstructions();
            base.ExitScreen();
        }
    
    }

}
